﻿using System;

namespace SimplexSolver
{
    public class SolverComposition
    {
        public event EventHandler<MessageTransferNeededEventArgs> MessageTransferNeeded;

        private readonly ScalarSimplexSolver _scalarSolver;

        private TargetParametrizedSimplexSolver _targetParametrizedSolver;

        private FreeValueParametrizedSimplexSolver _freeValueParametrizedSolver;

        public SolverComposition()
        {
            _scalarSolver = new ScalarSimplexSolver();
            _scalarSolver.MessageTransferNeeded += Solver_MessageTransferNeeded;
            _targetParametrizedSolver = new TargetParametrizedSimplexSolver();
            _targetParametrizedSolver.MessageTransferNeeded += Solver_MessageTransferNeeded;
        }

        public ISolution SolveScalar(ILPTask task)
        {
            return _scalarSolver.Solve(task);
        }

        public ISolution SolveParamTarget(ILPTask task, double[] deltaC)
        {
            _targetParametrizedSolver.SetTask(task);
            _targetParametrizedSolver.SetDelta(deltaC);
            _targetParametrizedSolver.Solve();
            return _targetParametrizedSolver.Solutions;
        }

        public ISolution SolveParamFreeValues(ILPTask task, double[] deltaB)
        {
            return null;
        }

        private void OnMessageTransferNeeded(string message, MessageTransferNeededEventArgs.EMessageType type)
        {
            if (MessageTransferNeeded != null)
            {
                var e = new MessageTransferNeededEventArgs(message, type);
                MessageTransferNeeded(this, e);
            }
        }

        private void Solver_MessageTransferNeeded(object sender, MessageTransferNeededEventArgs e)
        {
            OnMessageTransferNeeded(e.Message, e.MessageType);
        }
    }
}